package com.mall507.fivepeaks.widget.xscrollimage;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hzh.frame.core.ImageFrame.BaseImage;
import com.mall507.fivepeaks.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 贺子航  2015/04/09 16:37
 * @description 滚动图片控件
 */
public class XScrollImageFM extends Fragment {
    private List<ImageView> mImageViews;
    private String[] mImageUrls; // 图片URL
    private List<View> dots;// 图片标题正文的那些点
    private ViewPager viewPager;
    private FrameLayout frameLayout;
    private LinearLayout viewid;
    private int currentItem = 0; // 当前图片的索引号
    private int defaultImage = R.drawable.base_image_default;//默认显示图片

    public XScrollImageFM() {

    }

    public static XScrollImageFM newInstance(String[] mImageUrls) {
        XScrollImageFM model = new XScrollImageFM();
        Bundle bu = new Bundle();
        bu.putStringArray("mImageUrls", mImageUrls);
        model.setArguments(bu);
        return model;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        mImageUrls = getArguments().getStringArray("mImageUrls");
        FrameLayout view = (FrameLayout) inflater.inflate(R.layout.fm_xscrollimage, null);
        viewPager = (ViewPager) view.findViewById(R.id.public_vp);
        frameLayout = (FrameLayout) view.findViewById(R.id.public_vp_f);
        viewid = (LinearLayout) view.findViewById(R.id.public_viewid);
        loadtopImage();
        return view;
    }

    /**
     * @author 贺子航  2015/04/09 16:37
     * @description 初始化头部滑动图片
     */
    public void loadtopImage() {
        mImageViews = new ArrayList<ImageView>();
        // 初始化图片资源
        for (int i = 0; i < mImageUrls.length; i++) {
            SimpleDraweeView imageView = new SimpleDraweeView(getActivity());
            if (!TextUtils.isEmpty(mImageUrls[i])) {
                BaseImage.getInstance().load(mImageUrls[i], imageView);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            } else {
                imageView.setImageResource(defaultImage);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            }
            mImageViews.add(imageView);
        }
        dots = new ArrayList<View>();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        frameLayout.setLayoutParams(
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, dm.heightPixels / 3));
        // 创建View
        if (mImageUrls != null && mImageUrls.length > 0) {
            View view = null;
            for (int i = 0; i < mImageUrls.length; i++) {
                view = new View(getActivity());
                FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(10, 10);
                param.leftMargin = 3;
                param.rightMargin = 3;
                view.setLayoutParams(param);
                view.setBackgroundResource(R.drawable.view_xscrollimage_point_up);
                dots.add(view);
                viewid.addView(view);
            }
        }

        viewPager.setAdapter(new MyAdapter());// 设置填充ViewPager页面的适配器
        // 设置一个监听器，当ViewPager中的页面改变时调用
        viewPager.setOnPageChangeListener(new MyPageChangeListener());
        Resources resources = getResources();
        dots.get(0).setBackgroundResource(R.drawable.view_xscrollimage_point_down);
    }


    /**
     * @author 贺子航  2015/04/09 16:37
     * @description 头部滑动图片 | 适配器
     */
    private class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mImageUrls.length;
        }

        /**
         * 从当前container中删除指定位置（position）的View
         **/
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        /**
         * 做了两件事，第一：将当前视图添加到container中，第二：返回当前View
         **/
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mImageViews.get(position));
            return mImageViews.get(position);
        }

        /**
         * 该函数用来判断instantiateItem(ViewGroup, int)函数所返回来的Key与一个页面视图是否是代表的同一个视图
         * (即它俩是否是对应的，对应的表示同一个View)
         * 返回值：如果对应的是同一个View，返回True，否则返回False。
         **/
        @Override
        public boolean isViewFromObject(View view, Object key) {
            return view == key;
        }
    }

    /**
     * @author 贺子航  2015/04/09 16:37
     * @description 头部滑动图片 | 滑动监听
     */
    private class MyPageChangeListener implements ViewPager.OnPageChangeListener {
        private int oldPosition = 0;

        public void onPageSelected(int position) {
            currentItem = position;
            dots.get(oldPosition).setBackgroundResource(R.drawable.view_xscrollimage_point_up);
            dots.get(position).setBackgroundResource(R.drawable.view_xscrollimage_point_down);
            oldPosition = position;
        }

        public void onPageScrollStateChanged(int arg0) {

        }

        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }
    }
}
